﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace MyGames
{
    public class Control
    {
        public Texture2D Texture;
        public int Width;
        public int Height;
        public Vector2 Position;
        public string Text;
        public float Depth;

        public Rectangle ButtonRectangle
        {
            get { return new Rectangle((int)Position.X, (int)Position.Y, Width, Height); }
        }

        public Control(Texture2D texture, Vector2 pos, float depth)
        {
            this.Texture = texture;
            this.Text = null;
            this.Position = pos;
            this.Width = texture.Width;
            this.Height = texture.Height;
            this.Depth = depth;
        }

        public Control(string text, Vector2 pos, int width, int height, float depth)
        {
            this.Texture = null;
            this.Text = text;
            this.Position = pos;
            this.Width = width;
            this.Height = height;
            this.Depth = depth;
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont font, Color clr, SpriteEffects effect)
        {
            if (Texture != null)
                spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Width, Height), null,
                    Color.White, 0f, Vector2.Zero, effect, Depth - 0.01f);

            if (!String.IsNullOrEmpty(Text) && font != null)
            {
                Vector2 size = font.MeasureString(Text);
                spriteBatch.DrawString(font, Text, new Vector2(Position.X + Width / 2, Position.Y + Height / 2), clr, 0f,
                    new Vector2(size.X / 2, size.Y / 2), 1f, SpriteEffects.None, Depth);
            }
        }
    }
}
